package ru.usu.gv.gui.component;

import ru.usu.gv.gui.analysis.IGraphAnalysVisualizer;

import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.Color;
import java.awt.Dimension;

/**
 * @author spupyrev
 * @param <T>
 */
public class GVSlider extends JPanel implements IGraphAnalysVisualizer
{
	private JSlider slider;
	private JTextField textField;
	private int value;

	public GVSlider(String label, int min, int max, int initValue, boolean enabled)
	{
		textField = new JTextField(String.valueOf(initValue));
		textField.setEnabled(false);
		textField.setDisabledTextColor(Color.BLACK);
		textField.setPreferredSize(new Dimension(30, 20));
		add(textField);
		setBorder(new javax.swing.border.TitledBorder(label));

		value = initValue;
		slider = new JSlider(min, max, initValue);
		int range = max - min;
		slider.setMinorTickSpacing(range / 10);
		slider.setMajorTickSpacing(slider.getMinorTickSpacing() * 2);
		slider.setPaintLabels(true);
		slider.setPaintTicks(true);
		slider.setValue(initValue);
		slider.setLabelTable(slider.createStandardLabels(range / 5));
		add(slider);
		slider.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e)
			{
				setValue(slider.getValue());
				textField.setText(String.valueOf(value));
			}
		});

		slider.setEnabled(enabled);
	}

	public void addChangeListener(ChangeListener l)
	{
		slider.addChangeListener(l);
	}

	public int getValue()
	{
		return value;
	}

	public void setValue(int value)
	{
		this.value = value;
		slider.setValue(value);
	}
}
